package com.zhwmall.product.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhwmall.common.entity.product.AttrEntity;
import com.zhwmall.common.entity.product.ProductAttrValueEntity;
import com.zhwmall.common.vo.spuSaveVo.BaseAttrs;
import com.zhwmall.product.dao.AttrDao;
import com.zhwmall.product.dao.ProductAttrValueDao;
import com.zhwmall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrDao attrDao;

    @Override
    public void saveProductAttrValue(Long spuId, List<BaseAttrs> baseAttrs) {
        if (CollUtil.isNotEmpty(baseAttrs)){
            List<ProductAttrValueEntity> attrValueEntities = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity();
                attrValueEntity.setSpuId(spuId);
                attrValueEntity.setAttrId(attr.getAttrId());
                AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                attrValueEntity.setAttrName(attrEntity.getAttrName());
                attrValueEntity.setAttrValue(attr.getAttrValues());
                attrValueEntity.setQuickShow(attr.getShowDesc());

                return attrValueEntity;
            }).collect(Collectors.toList());

            this.saveBatch(attrValueEntities);
        }
    }
}